﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;using System.Text;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;

namespace CardsServer.WCF
{
	public partial interface IService
{
[OperationContract]
string Cards_Add(Cards model ,int ACID);


[OperationContract]
string Cards_Update(Cards model ,int ACID);


[OperationContract]
string CardsBycno_Delete(string cno,int ACID);


[OperationContract]
Cards CardsBycno_Get(string cno,int ACID );


[OperationContract]
List<Cards> Cards_Get(string sortBy,int ACID);




}
public partial class Service : IService
{
#region  添加Cards表数据
/// <summary>
/// 添加Cards
/// </summary>
/// <param name="model">Cards实体</param>
/// <returns>空或错误</returns>
public string Cards_Add(Cards model,int ACID)
{
if(string.IsNullOrEmpty(model.cno))
{
return "『串号』不能为空！";
}
if(model.state==null )
{
return "『卡状态，0=未绑定,1=已绑定，-1=废弃』不能为空！";
}
if(string.IsNullOrEmpty(model.WarehouseID))
{
return "『仓库ID』不能为空！";
}
if(model.indate ==null )
{
return "『购入日期』不能为空！";
}
if(model.jg==null )
{
return "『购入价格』不能为空！";
}
if(string.IsNullOrEmpty(model.GoodsCode))
{
return "『产品编码』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.Cards.Count(p=>p.cno==model.cno)>0)
{
return "主键已存在！";
}
ctx.Cards.InsertOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 添加失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  添加Cards表数据事物用重构
/// <summary>
/// 添加Cards事物用重构
/// </summary>
/// <param name="model">Cards实体</param>
/// <returns>空或错误</returns>
private string Cards_Add(Cards model,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(string.IsNullOrEmpty(model.cno))
{
return "『串号』不能为空！";
}
if(model.state==null )
{
return "『卡状态，0=未绑定,1=已绑定，-1=废弃』不能为空！";
}
if(string.IsNullOrEmpty(model.WarehouseID))
{
return "『仓库ID』不能为空！";
}
if(model.indate ==null )
{
return "『购入日期』不能为空！";
}
if(model.jg==null )
{
return "『购入价格』不能为空！";
}
if(string.IsNullOrEmpty(model.GoodsCode))
{
return "『产品编码』不能为空！";
}
if(ctx.Cards.Count(p=>p.cno==model.cno)>0)
{
return "主键已存在！";
}
ctx.Cards.InsertOnSubmit(model);
return string.Empty;
}
#endregion



#region  修改Cards表数据
/// <summary>
/// 修改Cards
/// </summary>
/// <param name="model">Cards实体</param>
/// <returns>空或错误</returns>
public string Cards_Update(Cards model,int ACID)
{
if(string.IsNullOrEmpty(model.cno))
{
return "『串号』不能为空！";
}
if(model.state==null )
{
return "『卡状态，0=未绑定,1=已绑定，-1=废弃』不能为空！";
}
if(string.IsNullOrEmpty(model.WarehouseID))
{
return "『仓库ID』不能为空！";
}
if(model.indate ==null )
{
return "『购入日期』不能为空！";
}
if(model.jg==null )
{
return "『购入价格』不能为空！";
}
if(string.IsNullOrEmpty(model.GoodsCode))
{
return "『产品编码』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.Cards.Count(p=>p.cno==model.cno)!= 1)
{
return "未找到指定记录或记录不唯一！";
}
Cards Cardsmodel=ctx.Cards.Single(p => p.cno==model.cno);
if(!string.IsNullOrEmpty(model.cno))
{
Cardsmodel.cno=model.cno;
}
if(!string.IsNullOrEmpty(model.GoodsCode))
{
Cardsmodel.GoodsCode=model.GoodsCode;
}
if(model.ClientID!=null || model.ClientID.Equals(Guid.Empty))
{
Cardsmodel.ClientID=model.ClientID;
}
if(model.jg!=null )
{
Cardsmodel.jg=model.jg;
}
if(model.indate!=null )
{
Cardsmodel.indate=model.indate;
}
if(!string.IsNullOrEmpty(model.WarehouseID))
{
Cardsmodel.WarehouseID=model.WarehouseID;
}
if(model.state!=null )
{
Cardsmodel.state=model.state;
}
if(!string.IsNullOrEmpty(model.Remarks))
{
Cardsmodel.Remarks=model.Remarks;
}
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 修改失败!错误详细信息：" + ex.Message;
}
}
}
#endregion



#region  删除Cards表Bycno数据
/// <summary>
/// 删除CardsBycno
/// </summary>
/// <param name="cno">串号</param>
/// <returns>空或错误</returns>
public string CardsBycno_Delete(string cno,int ACID)
{
if(string.IsNullOrEmpty(cno))
{
return "『串号』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.Cards.Count(p=>p.cno==cno)!=1)
{
return null;
}
Cards model=ctx.Cards.Single(p => p.cno== cno);
ctx.Cards.DeleteOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 删除失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  删除Cards表Bycno数据[事物用重构]
/// <summary>
/// 删除CardsBycno[事物用重构]
/// </summary>
/// <param name="cno">串号</param>
/// <returns>空或错误</returns>
private string CardsBycno_Delete(string cno,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(string.IsNullOrEmpty(cno))
{
return "『串号』不能为空！";
}
if(ctx.Cards.Count(p=>p.cno==cno)!=1)
{
return null;
}
Cards model=ctx.Cards.Single(p => p.cno== cno);
ctx.Cards.DeleteOnSubmit(model);
return string.Empty;
}
#endregion



#region  获取Cards表Bycno数据
/// <summary>
/// 获取CardsBycno
/// </summary>
/// <param name="cno">串号</param>
/// <returns>当前表</returns>
public Cards CardsBycno_Get(string cno,int ACID)
{
if(string.IsNullOrEmpty(cno))
{
return null;
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.Cards.Count(p=>p.cno==cno)!=1)
{
return null;
}
return ctx.Cards.SingleOrDefault(p=>p.cno==cno);
}
catch (Exception ex)
{
return null;
}
}
}
#endregion



#region  获取Cards表数据
/// <summary>
/// 获取Cards
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<Cards></returns>
public List<Cards> Cards_Get(Expression<Func<Cards, bool>> predicate , string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.Cards.ToList();
}
 else 
{
return ctx.Cards.OrderBy(sortBy).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.Cards.Where(predicate).ToList();
}
 else 
{
return ctx.Cards.Where(predicate).OrderBy(sortBy).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  获取Cards表数据
/// <summary>
/// 获取Cards
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<Cards></returns>
public List<Cards> Cards_Get(string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
return Cards_Get(null,sortBy, ACID);
}
catch (Exception)
{
return null;
}
}
}
#endregion


#region  获取Cards表数据计数
/// <summary>
/// 获取Cards表数据计数
/// </summary>
/// <returns>List<Cards></returns>
public int Cards_GetCount(Expression<Func<Cards, bool>> predicate,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
return ctx.Cards.Count();

}
 else
{
return ctx.Cards.Where(predicate).Count();
}
}
catch 
{
return 0;
}
}
}
#endregion




#region  获取Cards分页表数据
/// <summary>
/// 获取Cards分页表数据
/// </summary>
/// <param name="pageIndex">当前页</param>
/// <param name="pageSize">页大小</param>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<Cards></returns>
public List<Cards> Cards_Getpage(int pageIndex, int pageSize,Expression<Func<Cards, bool>> predicate ,string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.Cards.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.Cards.OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.Cards.Where(predicate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.Cards.Where(predicate).OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  批量修改Cards表数据
/// <summary>
/// 批量修改Cards
/// </summary>
/// <param name="predicate">Expression<Func<Cards, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<Cards,Cards>> 更新表达式树</param>
/// <param name="returns">影响记录数</param>
public int Cards_batchUpdate(Expression<Func<Cards, bool>> predicate,Expression<Func<Cards, Cards>> updater,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.Cards.Update(predicate, updater);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量修改Cards表数据事物用重构
/// <summary>
/// 批量修改Cards事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<Cards, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<Cards,Cards>> 更新表达式树</param>
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int Cards_batchUpdate(Expression<Func<Cards, bool>> predicate,Expression<Func<Cards, Cards>> updater,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.Cards.Update(predicate, updater);
return ok;
}
#endregion


#region  批量删除Cards表数据
/// <summary>
/// 批量删除Cards
/// </summary>
/// <param name="predicate">Expression<Func<Cards, bool>></param> 查找表达式树
/// <param name="returns">影响记录数</param>
public int Cards_batchDel(Expression<Func<Cards, bool>> predicate,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.Cards.Delete(predicate);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量删除Cards表数据事物用重构
/// <summary>
/// 批量删除Cards事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<Cards, bool>></param> 查找表达式树
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int Cards_batchDel(Expression<Func<Cards, bool>> predicate,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.Cards.Delete(predicate);
return ok;
}
#endregion


}
}

